<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class CategoryModel extends Model
{
    protected $table = 'pre_category';

    public function addCategoryData($name,$property,$sort,$states,$time){

        $cm = new CategoryModel();

        $cm->name = $name;

        $cm->sort = $sort;

        $cm->states = $states;

        $cm->property = $property;

        $cm->created_at = $time;

        $cm->updated_at = $time;

        $res = $cm->save();

        return $res;
    }

    public function updateCategoryData($id,$name,$property,$sort,$states,$time){

        $cm = CategoryModel::find($id);

        if(empty($cm)){
            return $cm;
        }

        $cm->name = $name;

        $cm->sort = $sort;

        $cm->states = $states;

        $cm->property = $property;

        $cm->updated_at = $time;

        $res = $cm->save();

        return $res;
    }

    public function delCategoryData($id){

        $states = 9;

        $res = CategoryModel::where("id",$id)->update([
            "states"=>$states
        ]);

        return $res;
    }

    public function selectCategoryData($limit){

        $categorys = CategoryModel::where("states","!=",DELETE_STATUS)->paginate($limit);

        return $categorys;
    }

    public function getCategory($id){

        $res = CategoryModel::where("id",$id)->get();

        return $res;
    }

    public function getAllCategory(){

        return CategoryModel::select('id','name')->where("states","!=",DELETE_STATUS)->get();

    }
}
